package ${filePackageName};

import ${utilPackage}.ConstantUtil;
import ${beanCommonPackage}.Response;
import ${utilPackage}.MessageConstant;
import ${utilPackage}.MessageUtils;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * 拦截器
 * @author ${projectAuthor}
 * @version 1.0
 * @date ${nowDate}
 */
public class ManageInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
        
    	boolean flag = true;
        String token = request.getHeader(ConstantUtil.DEFAULT_AUTH_TOKEN);
        if(ObjectUtil.isEmpty(token)){
            //进入登录页面
            output(response, JSON.toJSONString(Response.authorizationDeny(MessageUtils.get(MessageConstant.USER_NOT_LOGIN))));
            return false;
        }

        return flag;
    }


    private void output(HttpServletResponse response,String msg){
        try{
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print(msg);
            out.flush();
            out.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception e) throws Exception {

    }
}
